package com.vslimit.servlet;

import com.vslimit.command.VoaCatCommand;
import com.vslimit.dto.VoaCatDTO;
import com.vslimit.util.FuniApplicationContext;
import com.vslimit.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: vslimit
 * Date: 12-4-5
 * Time: 下午6:21
 * To change this template use File | Settings | File Templates.
 */
public class VoaCatServlet extends HttpServlet {

    private VoaCatCommand voaCatCommand;

    @Override
    public void init() throws ServletException {
        voaCatCommand = (VoaCatCommand) FuniApplicationContext.getBean("voaCatCommand");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        Integer pid = StringUtil.isEmpty(req.getParameter("pid")) ? 0 : Integer.valueOf(req.getParameter("pid"));
        voaCatCommand.setPid(pid);
        List<VoaCatDTO> voaCatDTOList = voaCatCommand.findByPid();

        String url = pid == 0 ? "voaCatServlet?pid=":"voaUrlListServlet?catId=";

        StringBuffer sb = new StringBuffer();
        sb.append("<html>");
        sb.append("<body>");

        for (VoaCatDTO dto : voaCatDTOList) {
            sb.append(dto.getId());
            sb.append("\t");
            sb.append("<a href="+ url + dto.getId() + ">");
            sb.append(dto.getName());
            sb.append("</a>");
            sb.append("<br/>");
        }
        sb.append("</body>");
        sb.append("</html>");

        PrintWriter out = resp.getWriter();
        out.print(sb.toString());

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
